//
//  LJifenMallViewController.m
//  CloudClassroom
//
//  Created by dfws on 2019/10/25.
//  Copyright © 2019 dfws. All rights reserved.
//

#import "LJifenMallViewController.h"
#import "MLMSegmentManager.h"
#import "MLMSegmentHead.h"
//#import "JifenListModel.h"
#import "LJifenMallCommonVC.h"
#import <Masonry/Masonry.h>
#define kPageCount 20
#define SEGMENTHEIGHT 35
#define ADJUSTHEIGHT 30 //仅仅是为了调整MLMSegmentManager
#define kIconFont 64
//RGB的颜色转换
#define kUIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define kUIColorFromRGBA(rgbValue,a) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

#define kCommonColor kUIColorFromRGB(0xffffff)
#define kCommonBtnColor kUIColorFromRGB(0x0A93FF)
#define kCommonLineColor kUIColorFromRGB(0xB7B7B7)
#define kCommonTextColor_One  kUIColorFromRGB(0x303030)
#define kCommonlogLine  kUIColorFromRGBA(0x000000, 0.25)
#define kCommonGreenColor kUIColorFromRGB(0x7fca5b)
#define kCommonYellowColor kUIColorFromRGB(0xF7D68F)
#define kCommonGrayColor kUIColorFromRGB(0xE0E0E0)
#define kCommonBackgroundColor kUIColorFromRGB(0xf5f5f5)
#define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
#define RGBColor(r, g, b) RGBAColor(r,g,b,1.0)
#define kGlobalBg kUIColorFromRGB(0xf6f6f6)
#define kUIScreenSize [UIScreen mainScreen].bounds.size
#define kUIScreenWidth kUIScreenSize.width
#define kUIScreenHeight kUIScreenSize.height
//用屏幕比率进行适配
#define kUIFitSize(float)   (float*(kUIScreenWidth/375.0))
#define KProportion_Width(proportion) (proportion*(kUIScreenWidth/375.0))

#define kStatusBarH [UIApplication sharedApplication].statusBarFrame.size.height
#define kNavigationBarHeight  (kStatusBarH >20?88:64)
#define kTabbarHeight    (kStatusBarH >20?83:49)
#define kScreenBounds   [[UIScreen mainScreen]bounds]
#define kScreenWidth    [[UIScreen mainScreen]bounds].size.width
#define kScreenHeight   [[UIScreen mainScreen]bounds].size.height
#define kWidthScale    (kScreenWidth / 375.0f)
#define kHeightScale    (kScreenHeight / 568.0f)
#define  IMG(x)          [UIImage imageNamed:x]
@interface LJifenMallViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;
@property(nonatomic)NSArray  *titleAry;
@property(nonatomic)NSArray  *keysAry;
/////////////////////
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) CGRect imageViewFrame;
@property (nonatomic) UILabel *availableJifenLabel;
@property (nonatomic) UILabel *thisYearXinzengjifenLabel;
@property (nonatomic) UILabel *willGuoQiJifenLabel;
@property (nonatomic) UILabel *willGuoQiTimeLabel;
@property(nonatomic)UIView *topBlackView;
@property(nonatomic)UIView *middleWhiteView;
@property(nonatomic)UIView *bottomJifenView;

@property (nonatomic,strong) UIScrollView *bgScrollView;
@property (nonatomic,strong) UIView *contentView;
///头部预留高度
@property (nonatomic,assign) CGFloat topHeight;

@property (nonatomic,strong) NSArray *vcsArray;

@property (nonatomic,assign) NSInteger nowIndex;
@property (nonatomic,strong) UIImageView *jifenImageV;

@end

@implementation LJifenMallViewController
#pragma mark ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    LJifenMallCommonVC *vc
    for (LJifenMallCommonVC *subVC in self.vcsArray) {
        subVC.canScrollHeight = _topHeight;
        subVC.nowRealScrollHeight = scrollView.contentOffset.y;
    }
    if (scrollView.contentOffset.y>_topHeight) {
        scrollView.contentOffset = CGPointMake(0, _topHeight);
    }
}
- (void)resetScrollEnable{
        if (self.vcsArray.count>_nowIndex) {
            LJifenMallCommonVC *subVC = self.vcsArray[_nowIndex];
            if (subVC.tableView.contentOffset.y>0) {
    //            scrollView.contentOffset = CGPointMake(0, _topHeight);
                NSLog(@"%lf",subVC.tableView.contentOffset.y);
                _bgScrollView.scrollEnabled = NO;
            }else{
                _bgScrollView.scrollEnabled = YES;
            }
        }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _topHeight = 200;
    _nowIndex = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleAry =  @[@"0-5000", @"5001-15000", @"15000以上"];
    self.keysAry = @[@"1",@"2",@"3"];
    self.bgScrollView = [[UIScrollView alloc]init];
    [self.view addSubview:self.bgScrollView];
    [_bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    _bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT+_topHeight);
    _bgScrollView.delegate = self;
    self.contentView = [UIView new];
    [_bgScrollView addSubview:self.contentView];
    
    
        [self.contentView addSubview:self.topBlackView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(_bgScrollView.contentSize.height));
    }];
    self.imageView.image = IMG(@"img_jfbj");
    self.imageView.backgroundColor = [UIColor redColor];
            
    [self.topBlackView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.right.top.equalTo(@0);
//                   make.top.equalTo(@(kNavigationBarHeight));
           make.height.equalTo(@(kUIFitSize(95+75)));
       }];
    self.topBlackView.backgroundColor = kUIColorFromRGB(0x282B34);
    
    [self.topBlackView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(@16);
           make.right.equalTo(@(-16));
           make.top.equalTo(@(16));
           make.bottom.equalTo(@0);
       }];
//            [self requestGetInfo];
            [self configureTopView];
            [self configureMiddleView];
            [self configureBottomView];
    [self segmentStyle];
    // Do any additional setup after loading the view.
}
#pragma mark -
- (void)segmentStyle {
    
    
    self.segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, kUIFitSize(246)+110+60, kUIScreenWidth, SEGMENTHEIGHT) titles:self.titleAry headStyle:SegmentHeadStyleLine layoutStyle:MLMSegmentLayoutDefault];
    self.segHead.fontScale = 1.0;
    self.segHead.deSelectColor =kUIColorFromRGB(0x9A9DA6);
    self.segHead.selectColor =  kUIColorFromRGB(0x333741);
    self.segHead.lineColor = kUIColorFromRGB(0xFFE384);
    self.segHead.headColor = [UIColor whiteColor];
    self.segHead.lineHeight = 2;
    self.segHead.lineScale = 0.5;
    self.vcsArray = [self vcArr:self.keysAry];
    self.segScroll = [[MLMSegmentScroll alloc] initWithFrame:
                      CGRectMake(
                                 0,
                                 CGRectGetMaxY(self.segHead.frame)+SEGMENTHEIGHT-ADJUSTHEIGHT,
                                 kUIScreenWidth,
                                 kUIScreenHeight
//                                 -CGRectGetMaxY(self.segHead.frame
//                                                                                                                                            )-SEGMENTHEIGHT+kStatusBarH
                                 ) vcOrViews:self.vcsArray];//
    __weak LJifenMallViewController *weakSelf = self;
    self.segScroll.scrollEnd = ^(NSInteger index) {
        weakSelf.nowIndex = index;
    };
    self.segScroll.loadAll = YES;
    [MLMSegmentManager associateHead:self.segHead withScroll:self.segScroll completion:^{
        [self.contentView addSubview:self.segHead];
        [self.contentView addSubview:self.segScroll];
        [_segHead mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@[_contentView,_segScroll]);
            make.top.equalTo(_jifenImageV.mas_bottom);
            make.height.equalTo(@70);
        }];
        [_segScroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_contentView);
            make.top.equalTo(_segHead.mas_bottom);
        }];
    }];
    //    self.view.backgroundColor = [UIColor redColor];
    
}
#pragma mark - 数据源
- (NSArray *)vcArr:(NSArray *)keysAry {
    NSMutableArray *arr = [NSMutableArray array];
    __weak LJifenMallViewController *weakSelf = self;
    for (NSInteger i = 0; i < keysAry.count; i ++) {
            LJifenMallCommonVC *vc = [LJifenMallCommonVC new];
            vc.range_type = self.keysAry[i];
            vc.scrollBolock = ^(CGFloat y) {
                [weakSelf resetScrollEnable];
            };
            [arr addObject:vc];
    
    }
    return arr;
}
//- (void)requestGetInfo
//{
////    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    WS(weakSelf);
//    NSDictionary *post = @{@"user_ticket":kUserTicket};
//    [[LMSAPIClient apiManager]requestAPI:LMSAPITypeUserCreditInfo parameters:post success:^(NSURLSessionDataTask *dataTask, id JSON) {
////        [MBProgressHUD hideHUDForView:self.view];
////
//        if ([JSON[@"status"] integerValue]==1) {
//            if ([JSON[@"data"] isNotNullOrEmpty]) {
//                self.availableJifenLabel.text = [NSString stringWithFormat:@"%@",JSON[@"data"][@"credit"]];
//                self.thisYearXinzengjifenLabel.text = [NSString stringWithFormat:@"%@",JSON[@"data"][@"year_credit"]];
//                self.willGuoQiJifenLabel.text = [NSString stringWithFormat:@"%@",JSON[@"data"][@"expire_credit"][@"credit"]];
//                 self.willGuoQiTimeLabel.text = [NSString stringWithFormat:@"%@过期积分",JSON[@"data"][@"expire_credit"][@"expire_time"]];
//            }
//
//        }
//
//    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
//
//    }];
//}
- (void)configureTopView
{
    UILabel *availableJifenIndicatorLabel = [[UILabel alloc] init];
    availableJifenIndicatorLabel.textColor = kUIColorFromRGB(0xffffff);
    availableJifenIndicatorLabel.font = [UIFont systemFontOfSize:14];
    availableJifenIndicatorLabel.text = @"可用积分";
    [self.imageView addSubview:availableJifenIndicatorLabel];
    [availableJifenIndicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@14);
        make.left.equalTo(@20);
    }];
    self.availableJifenLabel = [[UILabel alloc]init];
    self.availableJifenLabel.font = [UIFont boldSystemFontOfSize:20];
    self.availableJifenLabel.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.availableJifenLabel];
    [self.availableJifenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@37);
        make.left.equalTo(@20);
    }];
//    self.availableJifenLabel.text = @"9999";

    self.thisYearXinzengjifenLabel = [[UILabel alloc]init];
    [self.imageView addSubview:self.thisYearXinzengjifenLabel];
    [self.thisYearXinzengjifenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.availableJifenLabel);
        make.bottom.equalTo(@(-36));
    }];
    self.thisYearXinzengjifenLabel.font = [UIFont boldSystemFontOfSize:20];
//    self.thisYearXinzengjifenLabel.text = @"899999";
    
    
    UILabel *thisYearIndicatorLabel = [[UILabel alloc] init];
    [self.imageView addSubview:thisYearIndicatorLabel];
    [thisYearIndicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo((self.availableJifenLabel));
        make.bottom.equalTo(@(-12));
    }];
    thisYearIndicatorLabel.text = @"本年度新增积分";
    thisYearIndicatorLabel.font = [UIFont systemFontOfSize:13];
    thisYearIndicatorLabel.textColor = kUIColorFromRGB(0x272727);
    
    
    self.willGuoQiJifenLabel = [[UILabel alloc] init];
    self.willGuoQiJifenLabel.textColor = kUIColorFromRGB(0x272727);
    self.willGuoQiTimeLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.imageView addSubview:self.willGuoQiJifenLabel];
    [self.willGuoQiJifenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.thisYearXinzengjifenLabel);
        make.right.equalTo(@(-128));
    }];
//    self.willGuoQiJifenLabel.text = @"20000";
    
    
    self.willGuoQiTimeLabel = [[UILabel alloc] init];
    self.willGuoQiTimeLabel.textColor = kUIColorFromRGB(0x272727);
    self.willGuoQiTimeLabel.font = [UIFont systemFontOfSize:13];
    [self.imageView addSubview:self.willGuoQiTimeLabel];
    [self.willGuoQiTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(thisYearIndicatorLabel);
        make.left.equalTo((self.willGuoQiJifenLabel));
    }];
//    self.willGuoQiTimeLabel.text = @"XXXXXXX过期积分";
    
}



- (void)configureMiddleView
{
    [self.contentView addSubview:self.middleWhiteView];
    [self.middleWhiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topBlackView.mas_bottom);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@118);
    }];
    UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 110)];
    [self.middleWhiteView addSubview:whiteView];
    NSArray *titleArray = @[@"积分申报",@"积分记录",@"我的订单"];
    NSArray *imageArray = @[IMG(@"icon_integral"),IMG(@"icon_notes"),IMG(@"icon_order")];
    
    for (int i = 0; i < titleArray.count; i++) {
        UIView *placeHolderView = [[UIView alloc]initWithFrame:CGRectMake(i*kUIScreenWidth/3, 16, kUIScreenWidth/3, 110)];
        [whiteView addSubview:placeHolderView];
        UIImageView *iconImageV = [[UIImageView alloc] init];
        [placeHolderView addSubview:iconImageV];
        iconImageV.userInteractionEnabled = YES;
        iconImageV.image = imageArray[i];
        
        [iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(@0);
            make.top.equalTo(@0);
            make.height.equalTo(@(kIconFont));
            make.width.equalTo(@(kIconFont));
        }];
        UILabel *indicatorLabel = [[UILabel alloc]init];
//        indicatorLabel.font = [UIFont systemFontOfSize:12];
        [placeHolderView addSubview:indicatorLabel];
        [indicatorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(@0);
            make.bottom.equalTo(@(-30));
        }];
        indicatorLabel.textColor = kUIColorFromRGB(0x333741);
        indicatorLabel.text = titleArray[i];
        
//        switch (i) {
//            case 0:
//            {
//                [placeHolderView addTapGesture:^{
//                    NSLog(@"积分申报");
//                }];
//            }
//                break;
//            case 1:
//            {
//                [placeHolderView addTapGesture:^{
//                    NSLog(@"积分记录");
//                }];
//            }
//                break;
//                
//            default:
//            {
//                [placeHolderView addTapGesture:^{
//                    NSLog(@"我的订单");
//                }];
//            }
//                break;
//        }
        
        
    }
    
    UIView *indicatorView = [UIView new];
    [self.middleWhiteView addSubview:indicatorView];
    indicatorView.backgroundColor = kUIColorFromRGB(0xf5f5f5);
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@8);
        make.bottom.equalTo(@0);
    }];
}
- (void)configureBottomView
{
    UIImageView *jifenImageV = [[UIImageView alloc]init];
    _jifenImageV = jifenImageV;
    [self.contentView addSubview:jifenImageV];
    jifenImageV.image = IMG(@"icon_title_integarl");
    [jifenImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.middleWhiteView.mas_bottom).offset(20);
        make.width.equalTo(@80);
        make.height.equalTo(@20);
        make.left.equalTo(@16);
    }];
}
//- (void)layoutSubviews {
//    [super layoutSubviews];
//
////    self.imageViewFrame = self.bounds;
////    self.nickLabel.frame = CGRectMake(10, self.bounds.size.height - 30, 200, 20);
//}

//- (void)scrollViewDidScroll:(CGFloat)contentOffsetY {
//    CGRect frame = self.imageViewFrame;
//    frame.size.height -= contentOffsetY;
//    frame.origin.y = contentOffsetY;
//    self.imageView.frame = frame;
//}

- (UIImageView *)imageView
{
    if (!_imageView) {
        self.imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}
- (UIView *)topBlackView
{
    if (!_topBlackView) {
        self.topBlackView = [UIView new];
        
    }
    return _topBlackView;
}
- (UIView *)middleWhiteView
{
    if (!_middleWhiteView) {
        self.middleWhiteView = [UIView new];
    }
    return _middleWhiteView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
